<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
use DateTimeInterface;

class Cms_Custom_Follow extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable, HasFactory;

    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'cms_custom_follow';

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'user_id',
        'custom_id',
        'content',
        'is_del'
    ];

    protected $hidden = [
        'is_del',
    ];

    public function scopeNodel($query)
    {
        return $query->where('is_del', 'N');
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function hasOneUser()
    {
        return $this->hasOne('App\Models\Sys_User','id','user_id');
    }

    public function hasOneCustom()
    {
        return $this->hasOne('App\Models\Cms_Custom','id','custom_id');
    }

    public static function getFilter($filter)
    {
        return self::where(function ($query) use ($filter){

            if(isset($filter['user_id']) && !empty($filter['user_id'])){
                $query->where('user_id',$filter['user_id']);
            }
            if(isset($filter['custom_id']) && !empty($filter['custom_id'])){
                $query->where('custom_id',$filter['custom_id']);
            }
            if(isset($filter['content']) && !empty($filter['content'])){
                $query->where('content','LIKE','%'.$filter['content'].'%');
            }
        });
    }
}
